// pages/forgetPassword/forgetPassword.js
var util = require('../../utils/util.js')
var app = getApp()
var rootUrl = app.globalData.rootDocment
var imgPublicUrl = rootUrl + 'Public/Home/images/'
var flag = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
	  sendmsgclass: "", //验证码按钮class
	  getmsg: "获取验证码", //验证码按钮标题
	  verifcode: 0,	//服务器验证码
	  sendtp: true,	//发送状态
    telPhoneNumber: '',
    verifyInput: '',
    imageArr: {
      forgetPasswordOneImg: imgPublicUrl + 'forgetPasswordOneImg.png',
      forgetPasswordTwoImg: imgPublicUrl + 'forgetPasswordTwoImg.png'
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	 app.publicchangenavigate()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
	return app.globalshare();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //手机号
  telPhoneNumberInput: function (e) {
    this.setData({
      telPhoneNumber: e.detail.value,
    })
  },
  // 验证码
  verifyInput: function (e) {
	 if (e.detail.value) {
		 this.setData({
			 verifyInput: e.detail.value,
		 })
	 } else {
		 this.setData({
       verifyInput: ''
		 })
	 }
  },
  formSubmit: function (e) {
    var warn = "";
    var that = this;
	 //组合验证码
   var bdverifycode = that.data.verifyInput
   var bdverifycodeInt = parseInt(bdverifycode)  //取整数

	 if (e.detail.value.telPhoneNumber == "") {
      warn = "请填写您的手机号！";
   } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.telPhoneNumber))) {
      warn = "手机号格式不正确";
   } else if (bdverifycode == '' || bdverifycodeInt == 0) {
      warn = "请输入验证码";
	 } else if (bdverifycodeInt != that.data.verifcode) {	//	验证码是否正确
		 warn = "验证码错误";
	 } else {
      flag = true;
    }

	 //执行结果
	 if (flag) {
		 //关闭当前页面，跳转到重置密码页面。。。
		 wx.redirectTo({
			 url: '/pages/forgetPassword/forgetPasswordNext/forgetPasswordNext?PhoneNum=' + e.detail.value.telPhoneNumber
		 })
	 } else {
		 wx.showModal({
			 title: '提示',
			 content: warn
		 })
	 }
  },
  //获取手机验证码
  getVerifCode: function (e) {
	  var that = this
	  var timer = that.data.sendtp
	  if (timer) {
		  var telPhone = that.data.telPhoneNumber
		  //验证手机是否为空
		  if (telPhone == "") {
			  wx.showModal({
				  title: '提示',
				  content: "请填写您的手机号！"
			  })
		  } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(telPhone))) {
			  wx.showModal({
				  title: '提示',
				  content: "手机号格式不正确"
			  })
		  } else {
			  var time = 120  //倒计时总时间
			  that.setData({
				  sendmsgclass: "active",
				  getmsg: "发送中...",
				  sendtp: false
			  })
			  //去服务端验证是否是正常用户
			  util.AJAX('Home/Login/judgmentPhone', { telPhoneNumber: telPhone }, function (ress) { //方法体
				  if (ress.data.StatusCode == 100) {  //发送成功
					  //去服务端发送短信验证码
					  util.AJAX('Home/Login/sendPhoneCode', { mobile: telPhone }, function (res) { //方法体
						  if (res.data.status == 100) {  //发送成功
							  //锁定手机号
							  //更改验证码值
							  that.setData({
								  verifcode: res.data.verify,
								  phdisabletp: true
							  })
							  //开始计时
							  var inter = setInterval(function () {
								  that.setData({
									  sendmsgclass: "active",
									  getmsg: time + "s",
								  })
								  time--
								  if (time < 0) {
									  clearInterval(inter)
									  that.setData({
										  sendmsgclass: "",
										  getmsg: "获取验证码",
										  sendtp: true
									  })
								  }
							  }, 1000)
						  } else {	//发送失败
							  that.setData({
								  sendmsgclass: "",
								  getmsg: "获取验证码",
								  sendtp: true
							  })
						  }
					  }, 'POST')
				  } else {	//错误的用户手机号
					  that.setData({
						  sendmsgclass: "",
						  getmsg: "获取验证码",
						  sendtp: true
					  })
					  wx.showModal({
						  title: '提示',
						  content: ress.data.Msg
					  })
				  }
			  }, 'POST')
		  }

	  }
  }
})